#!/usr/bin/python
import os
import os.path
import sys

def ListFile(dir = os.getcwd()):
    """ list all files in one dir (recursive)  os.walk module provide the function"""
    if not os.access(dir, os.R_OK):
        print "%s cannot be read" %dir
        return
    for f in os.listdir(dir):
        file = os.path.join(dir, f)
        if os.path.isfile(file):
            print "there is %s in %s dir" %(file, dir)
        if os.path.isdir(file):
            ListFile(file)

def ListFile_oswalk(dir = os.getcwd()):
    """ list all files in one dir (recursive)"""
    if not os.access(dir, os.R_OK):
        print "%s cannot be read" %dir
        return
    for dir, dirs, files in os.walk(dir):
        print "in %s dir ,there is %s files" %(dir, files)
    sys.exit(1)
    
def ListFile_topdown(dir = os.getcwd()):
    """ list all files in one dir (recursive)  os.walk module provide the function"""
    if not os.access(dir, os.R_OK):
        print "%s cannot be read" %dir
        return
    files = os.listdir(dir)
    dirs, nondirs = [], []
    for file in files:
        if os.path.isdir(os.path.join(dir, file)):
            dirs.append(file)
        else:
            nondirs.append(file)
    print "current dir is %s ,there is %s files" %(dir, nondirs)
    
    if  len(dirs) != 0:
        for d in dirs:
            d = os.path.join(dir, d)
            ListFile_topdown(d)
            
if __name__ == '__main__':
    dir = sys.argv[1]
    ListFile_topdown(dir)
